#pragma once
#include "hxlbmpfile.h"
#include <iostream>
#include <string>
#include <fstream>

template <typename T>
bool array2txt(T &H, const char *cFilename) // 将一个数组输出到以cFilename命名的txt文件中
{
    std::ofstream outfile;
    outfile.open(cFilename, std::ios::out);
    if (!outfile.is_open())
    {
        std::cout << "Open file failure\n";
        outfile.close();
        return false;
    }

    for (int i = 0; i < (sizeof(H) / sizeof(H[0])); i++) // 遍历数组写入txt
    {
        outfile << H[i] << '\n';
    }
    outfile.close();
    return true;
}

template <typename T>
bool array2txt(T *H, int start, int length, const char *cFilename) //以指针传入数组，并且指定起始位置（有些数组[0]保留），数组长度需给定。
{
    std::ofstream outfile;
    outfile.open(cFilename, std::ios::out);
    if (!outfile.is_open())
    {
        std::cout << "Open file failure\n";
        outfile.close();
        return false;
    }

    for (int i = start; i <= length; i++) // 遍历数组写入txt
    {
        outfile << H[i] << '\t';
    }
    outfile.close();
    return true;
}

bool array2txt(const std::string &H, const char *cFilename); // 针对字符数组的输出函数

float *outHistogramData(HXLBMPFILE &src, const char *cFilename); // 将绘制“灰阶-概率”直方图的数据保存到txt文件中

float *HistEqualize(HXLBMPFILE &src, HXLBMPFILE &des, const char *cFilename); // 将src图像的直方图均衡化，并输出结果图像des

float *HistEqualize(HXLBMPFILE &src, HXLBMPFILE &des, float *hist_data, const char *cFilename); // 已知灰阶-概率矩阵，做直方图均衡化

float calculateA(); // 计算 y=a(1/4-(x-1/2)^2)中的a

float *getTargetHist(const char *cFilename); // 由函数y=a(1/4-(x-1/2)^2)得到目标直方图，并返回灰阶-概率数组

int *HistSpecificate(HXLBMPFILE &src, HXLBMPFILE &des, float *hist_data, const char *cFilename); // 直方图规定化